<?php
namespace Apipad\Controller;
use Think\Controller\RestController;
use Think\Controller;
header("content-type:application/json;charset=utf-8");
header("Access-Control-Allow-Origin:*");
class LoginController extends RestController
{

    //初始化
    function _initialize() {
         //清除过期验证码
         $verification_code = M("verification_code");
         $wher2=[];
         $wher2['times']=['lt',time()];
         $verification_code->where($wher2)->delete();
    }
 
    /** 图片上传*/
    function upfile() {
        $data = $this->upload();
        foreach($data['url'] as $key=>$val){
            $list[$key] = $this->getPicUrl($val);
        }
        $this->ajaxSucReturn($list);
    }

    public function Systemcc()
    {
        $systemInfo = M("system");
        $rs_systemInfo = $systemInfo->where("sId=1")->field('sLogo,sName,sCopyrightName')->find();
        $data['code']=200;
        $data['msg']='查询成功';
        $data['data']['info']=$rs_systemInfo;
        exit(json_encode($data));
    }
    //登录检查(客户)
    public function uchecklogin(){
        $u = D('User');
        //登录验证
        $post = I('post.');
        $form = $post['form'] && $post['form'] < 4 ? $post['form'] : 1;
        $system = M("system");
        $rs_system = $system->field("sCheckCodeSwitch,sErrorPwdLockNum,sLoginTimeout,order_contral")->where("sId=1")->find(); 
        if (!$post['aUser']) {
            $this->ajaxReturn(array('code' => 0, 'msg' => '请输入登录账号'), 'JSON');
        }
        if (!$post['aPwd']) {
            $this->ajaxReturn(array('code' => 0, 'msg' => '请输入登录密码'), 'JSON');
        }
        if (!$post['code']) {
            $this->ajaxReturn(array('code' => 0, 'msg' => '请输入验证码'), 'JSON');
        }
        
        if($rs_system["sCheckCodeSwitch"] == 1) {
            $verification = M("verification_code");
            $code_info=$verification->where(['codes_name'=>$post['code']])->find();
            if (empty($code_info)) {
                $data['code']=0;
                $data['msg']='验证码错误！';
                exit(json_encode($data));
            }
            if(time()>$code_info['times']){
                $data['code']=0;
                $data['msg']='验证码已过期！';
                exit(json_encode($data));
            }
        }
      
        $user = $u->login($form, $post);
        // 不存在此用户
        if (!$user) {
            $this->ajaxReturn(array('code' => 0, 'msg' => $u->getError()));
        }
        unset($user['password']);
        //查看是否登录成功更新最后一次登录信息
        if ($user) { //如果设置了用户微信信息，就清除微信信息
            //如果之前没有链接，就跳转到用户中心
            $admin['uid'] = $user['uid'];
            $admin['token'] = $user['token'];
            $admin['types'] = $user['types'];
            $admin['names'] = $user['names'];
            $admin['avatar'] = $user['avatar'] ?: 'https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/03/02/6400606a88a09.jpg';
            $stype = $admin['types'];
            $this->ajaxReturn(array('code' => 200, 'msg' => '登录成功', 'data' => $admin), 'JSON');
        } else {
            $this->ajaxReturn(array('code' => 0, 'msg' => '登录失败，请稍后重试'), 'JSON');
        }
    }

     //登录检查(客户)
     public function ucheckloginc(){
        $u = D('User');
        $dfa='';
        $fdsd="1";
        //登录验证
        $post = I('post.');
        $form = $post['form'] && $post['form'] < 4 ? $post['form'] : 1;
        if(!$post['aUser']){
            $this->ajaxReturn(array('code' => 0, 'msg' => '请输入登录账号'), 'JSON');
        }
        if(!$post['uid']){
            $this->ajaxReturn(array('code' => 0, 'msg' => '参数错误'), 'JSON');
        }
        $user = $u->loginc($form, $post);
        // 不存在此用户
        if (!$user) {
            $this->ajaxReturn(array('code' => 0, 'msg' => $u->getError()));
        }
        //查看是否登录成功更新最后一次登录信息
        if ($user) { //如果设置了用户微信信息，就清除微信信息
            //如果之前没有链接，就跳转到用户中心
            $admin['uid'] = $user['uid'];
            $admin['token'] = $user['token'];
            $admin['types'] = $user['types'];
            $admin['names'] = $user['names'];
            $admin['avatar'] = $user['avatar'] ?: 'https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/03/02/6400606a88a09.jpg';
            $stype = $admin['types'];
            $this->ajaxReturn(array('code' => 200, 'msg' => '登录成功', 'data' => $admin), 'JSON');
        } else {
            $this->ajaxReturn(array('code' => 0, 'msg' => '登录失败，请稍后重试'), 'JSON');
        }
    }

    //验证二级密码
    public function check_pass(){
        $u = D('User');
        $detail = I('post.');
        $form = $detail['form'] && $detail['form'] < 4 ? $detail['form'] : 1;
        if(empty($detail['token'])) {
            $this->ajaxReturn(array('code' => 0, 'msg' => 'token参数错误'), 'JSON');
        }
        if(empty($detail['password'])) {
            $this->ajaxReturn(array('code' => 0, 'msg' => '请输入密码'), 'JSON');
        }
        $user = $u->login_cc($form, $detail);
        if(!$user){
            $msg=$u->getError();
            if($msg=="客户账号不存在"){
                $this->ajaxReturn(array('code' => 101, 'msg' => $u->getError()));
            }else{
                $this->ajaxReturn(array('code' => 0, 'msg' => $u->getError()));
            }
        }
        unset($user['password']);
        unset($user['pass_word']);
        unset($user['salt']);
        unset($user['salts']);
        if ($user) { 
            $admin['token_cc'] = $user['token_cc'];
            $this->ajaxReturn(array('code' => 200, 'msg' => '验证成功', 'data' => $admin), 'JSON');
        } else {
            $this->ajaxReturn(array('code' => 0, 'msg' => '验证失败'), 'JSON');
        }
    }


 
    //退出登录
    public function Login_out(){
        $detail = I('post.');
        $token=$detail['token'];
        $token_model= M("token");
        $user_token_model= M("user_token");
        $info=$token_model->where(['token'=>$token,'type'=>2])->find();
        if($info){
           $aId=$info['aId'];
           $result=$token_model->where(['token'=>$token])->delete();
           if($result){
                $user_token_model->where(['uids'=>$aId])->delete();
                $data_cc['code'] =200;
                $data_cc['msg'] = "退出成功";
                $data_cc['data'] =$result;
                exit(json_encode($data_cc));
           }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "操作失败";
                exit(json_encode($data_cc));
           }
        }else{
            $data_cc['code'] = 101;
            $data_cc['msg'] = "token不存在";
            exit(json_encode($data_cc));
        }
    }
    
    
 
    public function  Verify(){
        $data_cc['code'] = 200;
        $data_cc['msg'] = '查询成功';
        $data_cc['data']['image_code'] = $this->getCode(4,60,20);
        exit(json_encode($data_cc));
    }

   
    //验证码生产
    function getCode() {				 
        header('Content-type: image/gif');						//输出头信息
        $image_w=76;											//验证码图形的宽				
        $image_h=30;											//验证码图形的高
        $number=range(0,9);										//定义一个成员为数字的数组
        $character=range("Z","A");								//定义一个成员为大写字母的数组
        $result=$number; 			 
        $string="";												//初始化
        $len=count($result);									//新数组的长
        for($i=0;$i<4;$i++)
        {
            $new_number[$i]=$result[rand(0,$len-1)];			//在$result数组中随机取出6个字符
            $string=$string.$new_number[$i];					//生成验证码字符串
        }
        $verification = M("verification_code");
        $data['codes_name']=$string;
        $data['times']=time()+120;
        $result = $verification->add($data);  
        $check_image=imagecreatetruecolor($image_w,$image_h);	//创建图片对象
        $white=imagecolorallocate($check_image, 255, 255, 255);	
        $black=imagecolorallocate($check_image, 0, 0, 0);	
        imagefill($check_image,0,0,$white);//设置背景颜色为白色
        $line_color=imagecolorallocate($check_image, 255, 0, 0);//设直线的颜色
        imageline($check_image, 0, 10, 100,20, $line_color);//画线
        // for($i=0;$i<100;$i++)			 //加入100个干扰的黑点
        // {
        //     imagesetpixel($check_image, rand(0,$image_w), rand(0,$image_h),$black);
        // }
        for($i=0;$i<count($new_number);$i++)					//在背景图片中循环输出4位验证码
        {
            $x=mt_rand(1,7)+$image_w*$i/6;						//设定字符所在位置X坐标
            $y=mt_rand(1,$image_h/6);							//设定字符所在位置Y坐标
            //随机设定字符颜色
            $color=imagecolorallocate($check_image,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));
            //输入字符到图片中
            imagestring($check_image,5,$x,$y,$new_number[$i],$color);
        }
        imagepng($check_image);
        imagedestroy($check_image);
    }

      /**
     * 客户注册
     */
    function khregister($data){
        $salt= $this->GetRandStr(6);
        //客户编号
        $user = M("user");
        $num = $user->where(['types'=>0])->count();
        $data['customer_no'] = str_pad($num,6,"0",STR_PAD_LEFT);
        $data['issh'] = 0;
        $data['salt']=$salt;
        $data['types']=0;
        $data['status'] = 1;
        $data['password']=$this->getEncryptPassword($data['password'] ,$salt);
        $data['addtime']=date("Y-m-d H:i:s");
        return $user->add($data);
    }
   
}
